class Solution {
  public:
    /**
     * 代码中的类名、方法名、参数名已经指定，请勿修改，直接返回方法规定的值即可
     *
     *
     * @param s string字符串
     * @return bool布尔型
     */
    bool isValid(string s) {
        if(s.size()%2!=0)
        {
            return false;
        }
        stack<char> instack;
        for (int i = 0; i < s.size(); i++) {
            if (s[i] == '(' || s[i] == '{' || s[i] == '[') {
                instack.push(s[i]);
            } else {
                if (!instack.empty()) 
                {
                    char top = instack.top();
                    instack.pop();
                    if (top == '(' && s[i] != ')') {
                        return  false;
                    }
                    if (top == '[' && s[i] != ']') {
                        return false;
                    }
                    if (top == '{' && s[i] != '}') {
                        return false;
                    }
                }
            }
        }
        return instack.empty();
    }
};